﻿using KPIS.DBM;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class RollbackTransection : System.Web.UI.Page
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        FIM_MoneyRequests.FIM_MoneyRequests mr = new FIM_MoneyRequests.FIM_MoneyRequests();
        FIM_Payments.FIM_Payments pay = new FIM_Payments.FIM_Payments();
        Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtBgmYear.Text = main.GetSystemControls().default_bgm_year.ToString();
                rblType_SelectedIndexChanged(null, null);
            }
        }

        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rblType.SelectedValue)
            {
                case "1":
                    grdBgmRequest.Visible = true;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridBgmRequest();
                    break;
                case "2":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = true;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridBgmProjOper();
                    break;
                case "3":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = true;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridMoneyReq();
                    break;
                case "4":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = true;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridPayment();
                    break;
                case "5":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = true;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridPrePurchaseRequest();
                    break;
                case "6":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = true;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    LoadGridPurchaseRequest();
                    break;
                case "7":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = true;
                    grdDeliveryNote.Visible = false;
                    LoadGridPurchaseOrder();
                    break;
                case "8":
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = true;
                    LoadGridDelivery();
                    break;
                default:
                    grdBgmRequest.Visible = false;
                    grdBgmProjOper.Visible = false;
                    grdMoneyReq.Visible = false;
                    grdPayment.Visible = false;
                    grdPrePurchaseRequest.Visible = false;
                    grdPurchaseRequest.Visible = false;
                    grdPurchaseOrder.Visible = false;
                    grdDeliveryNote.Visible = false;
                    break;
            }
        }

        private void LoadGridBgmRequest()
        {
            grdBgmRequest.DataSource = GetBgmRequest();
            grdBgmRequest.DataBind();
        }

        private void LoadGridBgmProjOper()
        {
            grdBgmProjOper.DataSource = GetBgmProjOper();
            grdBgmProjOper.DataBind();
        }

        private void LoadGridMoneyReq()
        {
            grdMoneyReq.DataSource = GetMoneyReq();
            grdMoneyReq.DataBind();
        }

        private void LoadGridPayment()
        {
            grdPayment.DataSource = GetPayment();
            grdPayment.DataBind();
        }

        private void LoadGridPrePurchaseRequest()
        {
            grdPrePurchaseRequest.DataSource = GetPrePurchaseRequest();
            grdPrePurchaseRequest.DataBind();
        }

        private void LoadGridPurchaseRequest()
        {
            grdPurchaseRequest.DataSource = GetPurchaseRequest();
            grdPurchaseRequest.DataBind();
        }

        private void LoadGridPurchaseOrder()
        {
            grdPurchaseOrder.DataSource = GetPurchaseOrder();
            grdPurchaseOrder.DataBind();
        }

        private void LoadGridDelivery()
        {
            grdDeliveryNote.DataSource = GetDeliveryNote();
            grdDeliveryNote.DataBind();
        }

        private DataTable GetBgmRequest()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select rq.bgm_req_seq, rq.bgm_req_code, rq.bgm_req_name, rq.bgm_year
, rq.job_site_seq, j.job_site_name, rqe.total_amount
, rq.created_by, rq.created_when
, concat(coalesce(np.prefix_name, ''), emp.first_name, ' ', emp.last_name) as full_name

from bgm_request rq
left join (select rqe.bgm_req_seq
, sum(coalesce(rqe.total_amount, 0)) as total_amount
from bgm_request_expense rqe
group by rqe.bgm_req_seq
) rqe on rq.bgm_req_seq = rqe.bgm_req_seq
left join hrm_mp_job_sites j on rq.job_site_seq = j.job_site_seq
left join hrm_ps_employees emp on rq.created_by = emp.emp_seq
left join mas_name_prefixes np on emp.name_prefix_seq = np.name_prefix_seq
where rq.record_status = 'A'"

                + (nBgmYear != null ? " and rq.bgm_year = @bgm_year" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_year", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetBgmProjOper()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select rq.bgm_proj_oper_seq, rq.bgm_proj_oper_code, rq.bgm_proj_oper_name, rq.bgm_year
, rq.job_site_seq, j.job_site_name, rqe.total_amount
, rq.created_by, rq.created_when
, concat(coalesce(np.prefix_name, ''), emp.first_name, ' ', emp.last_name) as full_name

from bgm_project_operation rq
left join (select rqe.bgm_proj_oper_seq
, sum(coalesce(rqe.amount, 0)) as total_amount
from bgm_project_operation_expense rqe
group by rqe.bgm_proj_oper_seq
) rqe on rq.bgm_proj_oper_seq = rqe.bgm_proj_oper_seq
left join hrm_mp_job_sites j on rq.job_site_seq = j.job_site_seq
left join hrm_ps_employees emp on rq.created_by = emp.emp_seq
left join mas_name_prefixes np on emp.name_prefix_seq = np.name_prefix_seq
where rq.record_status = 'A'"

                + (nBgmYear != null ? " and rq.bgm_year = @bgm_year" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_year", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetMoneyReq()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select rq.money_req_seq, rq.ref_no, rq.money_req_name, rq.bgm_year
, rq.job_site_seq, j.job_site_name, rqe.total_amount
, rq.created_by, rq.created_when
, concat(coalesce(np.prefix_name, ''), emp.first_name, ' ', emp.last_name) as full_name

from fim_money_requests rq
left join (select rqe.money_req_seq
, sum(coalesce(rqe.amount, 0)) as total_amount
from fim_money_request_dtls rqe
group by rqe.money_req_seq
) rqe on rq.money_req_seq = rqe.money_req_seq
left join hrm_mp_job_sites j on rq.job_site_seq = j.job_site_seq
left join hrm_ps_employees emp on rq.created_by = emp.emp_seq
left join mas_name_prefixes np on emp.name_prefix_seq = np.name_prefix_seq
where rq.record_status = 'A'"

                + (nBgmYear != null ? " and rq.bgm_year = @bgm_year" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_year", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetPayment()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select rq.pay_seq, rq.ref_no, rq.pay_name, rq.bgm_year, rq.amount
, rq.created_by, rq.created_when
, concat(coalesce(np.prefix_name, ''), emp.first_name, ' ', emp.last_name) as full_name
, case when rq.contact_seq is not null then j.NameTh else concat(coalesce(np2.prefix_name, ''), emp2.first_name, ' ', emp2.last_name) end as payee

from fim_payments rq
left join IcmMasContact j on rq.contact_seq = j.ConId
left join hrm_ps_employees emp2 on rq.emp_seq = emp2.emp_seq
left join mas_name_prefixes np2 on emp2.name_prefix_seq = np2.name_prefix_seq
left join hrm_ps_employees emp on rq.created_by = emp.emp_seq
left join mas_name_prefixes np on emp.name_prefix_seq = np.name_prefix_seq
where rq.record_status = 'A'"

                + (nBgmYear != null ? " and rq.bgm_year = @bgm_year" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_year", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetPrePurchaseRequest()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select ppr.PprId, ppr.Code, ppr.Name, ppr.BgYear, ppr.RequestDate, ppri.Amount
, ppr.CreatedBy, ppr.CreatedWhen

from IcmPrePurchaseRequest ppr
left join (select PprId, sum(coalesce(Amount, 0)) as Amount
    from IcmPrePurchaseRequestItem
    group by PprId
    ) ppri on ppr.PprId = ppri.PprId
where ppr.RecordStatus = 'A'"

                + (nBgmYear != null ? " and ppr.BgYear = @BgYear" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@BgYear", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetPurchaseRequest()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select pr.PrId, pr.Code, pr.Name, pr.BgYear, pr.PurchaseDate, pri.Amount
, pr.CreatedBy, pr.CreatedWhen

from IcmPurchaseRequest pr
left join (select PrId, sum(coalesce(Amount, 0)) as Amount
    from IcmPurchaseRequestItem
    group by PrId
    ) pri on pr.PrId = pri.PrId
where pr.RecordStatus = 'A'"

                + (nBgmYear != null ? " and pr.BgYear = @BgYear" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@BgYear", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetPurchaseOrder()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select po.PoId, po.Code, po.Name, po.BgYear, c.NameTh, poi.Amount
, po.CreatedBy, po.CreatedWhen

from IcmPurchaseOrder po
left join IcmBidder bidder on po.BidderId = bidder.BidderId
left join IcmMasContact c on bidder.ConId = c.ConId
left join (select poi.PoId, sum(coalesce(poii.NetAmount, 0)) as Amount
    from IcmPurchaseOrderInstallment poi
    left join IcmPurchaseOrderInstallmentItem poii on poi.PoInstallId = poii.PoInstallId
    group by poi.PoId
    ) poi on po.PoId = poi.PoId
where po.RecordStatus = 'A'"

                + (nBgmYear != null ? " and po.BgYear = @BgYear" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@BgYear", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private DataTable GetDeliveryNote()
        {
            int? nBgmYear = main.ConvertNullInt(txtBgmYear.Value);

            string strSQL = @"
select dn.DnId, dn.Code, dn.Name, po.BgYear, c.NameTh, dni.Amount, dn.ReceiveDate
, dn.CreatedBy, dn.CreatedWhen

from IcmDeliveryNote dn
left join IcmPurchaseOrder po on dn.PoId = po.PoId
left join IcmBidder bidder on po.BidderId = bidder.BidderId
left join IcmMasContact c on bidder.ConId = c.ConId
left join (select DnId, sum(coalesce(NetAmount, 0)) as Amount
    from IcmDeliveryNoteItem
    group by DnId
    ) dni on dn.DnId = dni.DnId
where dn.RecordStatus = 'A'"

                + (nBgmYear != null ? " and po.BgYear = @BgYear" : "");

            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@BgYear", nBgmYear);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();
            return dtb;
        }

        private bool RollbackBgmRequest(int bgm_req_seq)
        {
            string strSQL = "";

            try
            {
                // update bgm_request
                strSQL = @"
update bgm_request
set record_status = 'W'
, updated_by = 1
, updated_when = getdate()
where bgm_req_seq = @bgm_req_seq
";
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@bgm_req_seq", bgm_req_seq);

                dbManager.BeginTransaction();
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                dbManager.RollBackTransaction();
                dbManager.Dispose();
                main.MessageAlert(Page, "[update bgm_request]" + ex.Message);
                return false;
            }

            dbManager.CommitTransaction();
            dbManager.Dispose();
            main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
            return true;
        }

        private bool RollbackBgmProjOper(int bgm_proj_oper_seq)
        {
            string strSQL = "";
            strSQL = @"
select ref_no as code, 'fim_money_requests' as table_name
from fim_money_requests
where bgm_proj_oper_seq = @bgm_proj_oper_seq
and record_status not like 'D'

union all

select ppr.Code as code, 'IcmPrePurchaseRequestItem' as table_name
from IcmPrePurchaseRequestItem ppri
left join IcmPrePurchaseRequest ppr on ppri.PprId = ppr.PprId
where ppri.ProjOperExpId = @bgm_proj_oper_seq
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@bgm_proj_oper_seq", bgm_proj_oper_seq);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region update bgm_project_operation
                try
                {
                    strSQL = @"
update bgm_project_operation
set record_status = 'W'
, updated_by = 1
, updated_when = getdate()
where bgm_proj_oper_seq = @bgm_proj_oper_seq
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_operation_report
                try
                {
                    strSQL = @"
update bgm_project_operation_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where bgm_proj_oper_exp_seq in (select bgm_proj_oper_exp_seq
	from bgm_project_operation_expense
	where bgm_proj_oper_seq = @bgm_proj_oper_seq
	)
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_report
                try
                {
                    strSQL = @"
update bgm_project_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where bgm_proj_oper_exp_seq in (select bgm_proj_oper_exp_seq
	from bgm_project_operation_expense
	where bgm_proj_oper_seq = @bgm_proj_oper_seq
	)
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_report]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] "; 
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackMoneyReq(int money_req_seq)
        {
            var mrInfo = mr.GetInfo(money_req_seq.ToString());
            string RefNo = mrInfo.ref_no;
            string DnId = mrInfo.icm_dn_seq.ToString();
            string strSQL = "";
            strSQL = @"
select ref_no as code, 'fim_payments' as table_name
from fim_payments
where money_req_seq = @money_req_seq
and record_status not like 'D'
";
//union all

//select tran_code as code, 'acm_transection' as table_name
//from acm_transection
//where tran_grp_seq = (select tran_grp_seq from fim_money_requests where money_req_seq = @money_req_seq)
//and record_status not like 'D'
//";
            dbManager.Open();
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@money_req_seq", money_req_seq);
            dbManager.AddParameters(1, "@DnId", DnId);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region Adjust Account
                strSQL = @"
select t.tran_seq as seq, t.tran_code as code, 'acm_transection' as table_name
from acm_transection t
inner join fim_money_requests mr on t.ref_no = mr.ref_no
where mr.money_req_seq = @money_req_seq
and t.record_status not like 'D'
";
                dbManager.BeginTransaction();
                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    try
                    {
                        string fiscalPeriod = DateTime.Today.Year.ToString() + "/" + DateTime.Today.Month.ToString();

                        foreach (DataRow dr in dtb.Rows)
                        {
                            int TranSeq = main.ConvertNullInt(dr["seq"].ToString()) ?? 0;
                            M_ACM_GENERAL_Ledger info = GetHeaderInfo(TranSeq, dbManager);
                            info.fiscal_period = fiscalPeriod;
                            info.tran_type = "A";
                            info.tran_date = DateTime.Today;
                            info.tran_grp_seq = null;
                            info.ref_no = GetNextTranCode(info.bgm_source_seq.ToString(), info.bgm_year.ToString(), "A", dbManager);
                            info.tran_name = "ปรับปรุงบัญชีจากการยกเลิกรายการรอเบิก " + RefNo;
                            info.record_status = "W";
                            info.created_by = SsMng.EmpSeqI;
                            info.created_when = DateTime.Now;

                            string NewTranSeq = Insert_Transaction(info, dbManager);

                            IList<M_ACM_GENERAL_Ledger> infoList = GetDtlsFullData(TranSeq, dbManager).OrderBy(o => o.dr_or_cr).ToList();
                            foreach (M_ACM_GENERAL_Ledger infoDtls in infoList)
                            {
                                infoDtls.tran_seq = main.ConvertNullInt(NewTranSeq) ?? 0;
                                infoDtls.dr_or_cr = infoDtls.dr_or_cr == "Dr" ? "Cr" : "Dr";
                                infoDtls.record_status = "W";
                                infoDtls.created_by = SsMng.EmpSeqI;
                                infoDtls.created_when = DateTime.Now;
                                Insert_DB_CR(infoDtls, dbManager);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        dbManager.RollBackTransaction();
                        main.MessageAlert(Page, "[adjust account]" + ex.Message);
                        return false;
                    }
                }
                #endregion

                #region update fim_money_requests
                try
                {
                    strSQL = @"
update fim_money_requests
set record_status = 'W'
, updated_by = 1
, updated_when = getdate()
where money_req_seq = @money_req_seq
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update fim_money_requests]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_operation_report
                try
                {
                    strSQL = @"
update bgm_project_operation_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where money_req_dtls_seq in (select money_req_dtls_seq
	from fim_money_request_dtls
	where money_req_seq = @money_req_seq
    and record_status not like 'D'
	)
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                    return false;
                }

                if (!string.IsNullOrEmpty(DnId))
                {
                    try
                    {
                        strSQL = @"
update rpt
set rpt.cal_amount = rpt.cal_amount - dni.NetAmount
from IcmDeliveryNoteItem dni
inner join IcmPurchaseOrderInstallmentItem poii on dni.PoInstallItemId = poii.PoInstallItemId
inner join IcmBidderQuotation q on poii.BidderQuoId = q.BidderQuoId
inner join bgm_project_operation_report rpt on q.PrItemId = rpt.icm_pr_item_seq and rpt.record_status <> 'D'
where dni.DnId = @DnId
";
                        dbManager.ExecuteNonQuery(CommandType.Text, strSQL);

//                        strSQL = @"
//select rpt.bgm_proj_oper_rpt_seq
//from IcmDeliveryNoteItem dni
//inner join IcmPurchaseOrderInstallmentItem poii on dni.PoInstallItemId = poii.PoInstallItemId
//inner join IcmBidderQuotation q on poii.BidderQuoId = q.BidderQuoId
//inner join bgm_project_operation_report rpt on q.PrItemId = rpt.icm_pr_item_seq
//where dni.DnId = @DnId
//";
//                        DataTable dtb2 = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

//                        strSQL = @"
//update bgm_project_operation_report
//set record_status = 'D'
//, updated_by = 1
//, updated_when = getdate()
//where bgm_proj_oper_rpt_seq in (select rpt.bgm_proj_oper_rpt_seq
//    from IcmDeliveryNoteItem dni
//    inner join IcmPurchaseOrderInstallmentItem poii on dni.PoInstallItemId = poii.PoInstallItemId
//    inner join IcmBidderQuotation q on poii.BidderQuoId = q.BidderQuoId
//    inner join bgm_project_operation_report rpt on q.PrItemId = rpt.icm_pr_item_seq
//    where dni.DnId = @DnId
//	)
//";

//                        foreach (DataRow dr2 in dtb2.Rows)
//                        {
//                            dbManager.CreateParameters(1);
//                            dbManager.AddParameters(0, "@money_req_seq", money_req_seq);
//                            dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
//                        }                        
                    }
                    catch (Exception ex)
                    {
                        dbManager.RollBackTransaction();
                        main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                        return false;
                    }
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackPayment(int pay_seq)
        {
            string RefNo = pay.GetInfo(pay_seq.ToString()).ref_no;
            string strSQL = "";
            strSQL = @"
select tran_code as code, 'acm_transection' as table_name
from acm_transection
where tran_grp_seq = (select tran_grp_seq from fim_payments where pay_seq = @pay_seq)
and record_status not like 'D'

union all

select ref_no as code, 'fim_payments' as table_name
from fim_payments
where (post_pay_seq = @pay_seq or tax_pay_seq = @pay_seq)
and record_status not like 'D'
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@pay_seq", pay_seq);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region Adjust Account
                strSQL = @"
select tran_seq as seq, tran_code as code, 'acm_transection' as table_name
from acm_transection
where tran_grp_seq = (select tran_grp_seq from fim_payments where pay_seq = @pay_seq)
and record_status not like 'D'
";
                dbManager.BeginTransaction();
                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    try
                    {
                        string fiscalPeriod = DateTime.Today.Year.ToString() + "/" + DateTime.Today.Month.ToString();

                        foreach (DataRow dr in dtb.Rows)
                        {
                            int TranSeq = main.ConvertNullInt(dr["seq"].ToString()) ?? 0;
                            M_ACM_GENERAL_Ledger info = GetHeaderInfo(TranSeq, dbManager);
                            info.fiscal_period = fiscalPeriod;
                            info.tran_type = "A";
                            info.tran_date = DateTime.Today;
                            info.tran_grp_seq = null;
                            info.ref_no = GetNextTranCode(info.bgm_source_seq.ToString(), info.bgm_year.ToString(), "A", dbManager);
                            info.tran_name = "ปรับปรุงบัญชีจากการยกเลิกรายการจ่ายเงิน " + RefNo;
                            info.record_status = "W";
                            info.created_by = SsMng.EmpSeqI;
                            info.created_when = DateTime.Now;

                            string NewTranSeq = Insert_Transaction(info, dbManager);

                            IList<M_ACM_GENERAL_Ledger> infoList = GetDtlsFullData(TranSeq, dbManager).OrderBy(o => o.dr_or_cr).ToList();
                            foreach (M_ACM_GENERAL_Ledger infoDtls in infoList)
                            {
                                infoDtls.tran_seq = main.ConvertNullInt(NewTranSeq) ?? 0;
                                infoDtls.dr_or_cr = infoDtls.dr_or_cr == "Dr" ? "Cr" : "Dr";
                                infoDtls.record_status = "W";
                                infoDtls.created_by = SsMng.EmpSeqI;
                                infoDtls.created_when = DateTime.Now;
                                Insert_DB_CR(infoDtls, dbManager);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        dbManager.RollBackTransaction();
                        main.MessageAlert(Page, "[adjust account]" + ex.Message);
                        return false;
                    }
                }
                #endregion

                #region update fim_payments
                try
                {
                    strSQL = @"
update fim_payments
set record_status = 'W'
, updated_by = 1
, updated_when = getdate()
where pay_seq = @pay_seq
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update fim_payments]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_operation_report
                try
                {
                    strSQL = @"
update bgm_project_operation_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where pay_seq = @pay_seq
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                    return false;
                }
                #endregion

                #region update fim_payment_cheques
                try
                {
                    strSQL = @"
update fim_payment_cheques
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where pay_seq = @pay_seq
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update fim_payment_cheques]" + ex.Message);
                    return false;
                }
                #endregion

                #region update fim_payments_payinpack
                try
                {
                    strSQL = @"
update fim_payments_payinpack
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where pay_seq = @pay_seq
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update fim_payments_payinpack]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackPrePurchaseRequest(int PprId)
        {
            string RefNo = pay.GetInfo(PprId.ToString()).ref_no;
            string strSQL = "";
            strSQL = @"
select pr.Code as code, 'IcmPurchaseRequest' as table_name
from IcmPrePurchaseRequestItem ppri
left join IcmPurchaseRequestItem pri on ppri.PrItemId = pri.PrItemId
left join IcmPurchaseRequest pr on pri.PrId = pr.PrId
where ppri.PprId = @PprId
and ppri.PrItemId is not null
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@PprId", PprId);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region update fim_payments
                try
                {
                    strSQL = @"
update IcmPrePurchaseRequest
set RecordStatus = 'W'
, UpdatedBy = 1
, UpdatedWhen = getdate()
where PprId = @PprId
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update IcmPrePurchaseRequest]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_operation_report
                try
                {
                    strSQL = @"
update bgm_project_operation_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where icm_ppr_item_seq in (select PprItemId
	from IcmPrePurchaseRequestItem
	where PprId = @PprId)
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackPurchaseRequest(int PrId)
        {
            string RefNo = pay.GetInfo(PrId.ToString()).ref_no;
            string strSQL = "";
            strSQL = @"
select po.Code as code, 'IcmPurchaseOrder' as table_name
from IcmPurchaseOrder po
left join IcmBidder bidder on po.BidderId = bidder.BidderId
where bidder.PrId = @PrId
and po.RecordStatus not like 'D'
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@PrId", PrId);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region update IcmPurchaseRequest
                try
                {
                    strSQL = @"
update IcmPurchaseRequest
set RecordStatus = 'W'
, UpdatedBy = 1
, UpdatedWhen = getdate()
where PrId = @PrId
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update IcmPurchaseRequest]" + ex.Message);
                    return false;
                }
                #endregion

                #region update IcmPrePurchaseRequestItem
                try
                {
                    strSQL = @"
update IcmPrePurchaseRequestItem
set PrItemId = null
, UpdatedBy = 1
, UpdatedWhen = getdate()
where PrItemId in (select PrItemId from IcmPurchaseRequestItem where PrId = @PrId)
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update IcmPrePurchaseRequestItem]" + ex.Message);
                    return false;
                }
                #endregion

                #region update bgm_project_operation_report
                try
                {
                    strSQL = @"
update bgm_project_operation_report
set record_status = 'D'
, updated_by = 1
, updated_when = getdate()
where icm_pr_item_seq in (select PrItemId
	from IcmPurchaseRequestItem
	where PrId = @PrId)
";
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update bgm_project_operation_report]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackPurchaseOrder(int PoId)
        {
            string RefNo = pay.GetInfo(PoId.ToString()).ref_no;
            string strSQL = "";
            strSQL = @"
select Code as code, 'IcmDeliveryNote' as table_name
from IcmDeliveryNote
where PoId = @PoId
and RecordStatus not like 'D'
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@PoId", PoId);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region update IcmPurchaseOrder
                try
                {
                    strSQL = @"
update IcmPurchaseOrder
set RecordStatus = 'W'
, UpdatedBy = 1
, UpdatedWhen = getdate()
where PoId = @PoId
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update IcmPurchaseOrder]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        private bool RollbackDeliveryNote(int DnId)
        {
            //string RefNo = pay.GetInfo(DnId.ToString()).ref_no;
            string strSQL = "";
            strSQL = @"
select tran_code as code, 'acm_transection' as table_name
from acm_transection
where tran_grp_seq = (select TranGrpId from IcmDeliveryNote where DnId = @DnId)
and record_status not like 'D'

union all

select ref_no as code, 'fim_money_requests' as table_name
from fim_money_requests
where icm_dn_seq = @DnId
and record_status not like 'D'
";
            dbManager.Open();
            dbManager.CreateParameters(1);
            dbManager.AddParameters(0, "@DnId", DnId);
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            if (dtb.Rows.Count == 0)
            {
                #region Adjust Account
                strSQL = @"
select tran_seq as seq, t.tran_code as code, 'acm_transection' as table_name, dn.Code
from acm_transection t
left join IcmDeliveryNote dn on t.tran_grp_seq = dn.TranGrpId
where t.tran_grp_seq = (select TranGrpId from IcmDeliveryNote where DnId = @DnId)
and t.record_status not like 'D'
";
                dbManager.BeginTransaction();
                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    try
                    {
                        string fiscalPeriod = DateTime.Today.Year.ToString() + "/" + DateTime.Today.Month.ToString();

                        foreach (DataRow dr in dtb.Rows)
                        {
                            int TranSeq = main.ConvertNullInt(dr["seq"].ToString()) ?? 0;
                            M_ACM_GENERAL_Ledger info = GetHeaderInfo(TranSeq, dbManager);
                            info.fiscal_period = fiscalPeriod;
                            info.tran_type = "A";
                            info.tran_date = DateTime.Today;
                            info.tran_grp_seq = null;
                            info.ref_no = GetNextTranCode(info.bgm_source_seq.ToString(), info.bgm_year.ToString(), "A", dbManager);
                            info.tran_name = "ปรับปรุงบัญชีจากการยกเลิกรายการตรวจรับ " + dr["Code"].ToString();
                            info.record_status = "W";
                            info.created_by = SsMng.EmpSeqI;
                            info.created_when = DateTime.Now;

                            string NewTranSeq = Insert_Transaction(info, dbManager);

                            IList<M_ACM_GENERAL_Ledger> infoList = GetDtlsFullData(TranSeq, dbManager).OrderBy(o => o.dr_or_cr).ToList();
                            foreach (M_ACM_GENERAL_Ledger infoDtls in infoList)
                            {
                                infoDtls.tran_seq = main.ConvertNullInt(NewTranSeq) ?? 0;
                                infoDtls.dr_or_cr = infoDtls.dr_or_cr == "Dr" ? "Cr" : "Dr";
                                infoDtls.record_status = "W";
                                infoDtls.created_by = SsMng.EmpSeqI;
                                infoDtls.created_when = DateTime.Now;
                                Insert_DB_CR(infoDtls, dbManager);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        dbManager.RollBackTransaction();
                        main.MessageAlert(Page, "[adjust account]" + ex.Message);
                        return false;
                    }
                }
                #endregion

                #region update IcmDeliveryNote
                try
                {
                    strSQL = @"
update IcmDeliveryNote
set RecordStatus = 'W'
, UpdatedBy = 1
, UpdatedWhen = getdate()
where DnId = @DnId
";
                    dbManager.BeginTransaction();
                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }
                catch (Exception ex)
                {
                    dbManager.RollBackTransaction();
                    main.MessageAlert(Page, "[update IcmDeliveryNote]" + ex.Message);
                    return false;
                }
                #endregion

                dbManager.CommitTransaction();
                main.MessageAlert(Page, "ย้อนสถานะเรียบร้อย");
                return true;
            }
            else
            {
                string strErrorId = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    strErrorId += "[Table = " + dr[1].ToString() + ", Code = " + dr[0].ToString() + "] ";
                }

                main.MessageAlert(Page, "รายการนี้ถูกนำไปดำเนินการต่อแล้ว โดย " + strErrorId);
                return false;
            }
        }

        protected void grdBgmRequest_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridBgmRequest();
            }
            else if (e.CommandName == "Rollback")
            {
                int bgm_req_seq = main.ConvertNullInt(((GridDataItem)e.Item)["bgm_req_seq"].Text) ?? 0;
                if (RollbackBgmRequest(bgm_req_seq))
                {
                    LoadGridBgmRequest();
                }
                else
                {
                }
            }
        }

        protected void grdBgmProjOper_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridBgmProjOper();
            }
            else if (e.CommandName == "Rollback")
            {
                int bgm_proj_oper_seq = main.ConvertNullInt(((GridDataItem)e.Item)["bgm_proj_oper_seq"].Text) ?? 0;
                if (RollbackBgmProjOper(bgm_proj_oper_seq))
                {
                    LoadGridBgmProjOper();
                }
                else
                {
                }
            }
        }

        protected void grdMoneyReq_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridMoneyReq();
            }
            else if (e.CommandName == "Rollback")
            {
                int money_req_seq = main.ConvertNullInt(((GridDataItem)e.Item)["money_req_seq"].Text) ?? 0;
                if (RollbackMoneyReq(money_req_seq))
                {
                    LoadGridMoneyReq();
                }
                else
                {
                }
            }
        }

        protected void grdPayment_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridPayment();
            }
            else if (e.CommandName == "Rollback")
            {
                int pay_seq = main.ConvertNullInt(((GridDataItem)e.Item)["pay_seq"].Text) ?? 0;
                if (RollbackPayment(pay_seq))
                {
                    LoadGridPayment();
                }
                else
                {
                }
            }
        }

        protected void grdDeliveryNote_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridDelivery();
            }
            else if (e.CommandName == "Rollback")
            {
                int DnId = main.ConvertNullInt(((GridDataItem)e.Item)["DnId"].Text) ?? 0;
                if (RollbackDeliveryNote(DnId))
                {
                    LoadGridDelivery();
                }
                else
                {
                }
            }
        }

        protected void grdPurchaseOrder_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridPurchaseOrder();
            }
            else if (e.CommandName == "Rollback")
            {
                int PoId = main.ConvertNullInt(((GridDataItem)e.Item)["PoId"].Text) ?? 0;
                if (RollbackPurchaseOrder(PoId))
                {
                    LoadGridPurchaseOrder();
                }
                else
                {
                }
            }
        }

        protected void grdPurchaseRequest_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridPurchaseRequest();
            }
            else if (e.CommandName == "Rollback")
            {
                int PrId = main.ConvertNullInt(((GridDataItem)e.Item)["PrId"].Text) ?? 0;
                if (RollbackPurchaseRequest(PrId))
                {
                    LoadGridPurchaseRequest();
                }
                else
                {
                }
            }
        }

        protected void grdPrePurchaseRequest_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridPrePurchaseRequest();
            }
            else if (e.CommandName == "Rollback")
            {
                int PprId = main.ConvertNullInt(((GridDataItem)e.Item)["PprId"].Text) ?? 0;
                if (RollbackPrePurchaseRequest(PprId))
                {
                    LoadGridPrePurchaseRequest();
                }
                else
                {
                }
            }
        }

        public void Insert_DB_CR(M_ACM_GENERAL_Ledger info, DBManager dbManager) //acm_transection_dtls
        {
            string strSQL;
            try
            {
                //dbManager.Open();
                dbManager.CreateParameters(7);
                dbManager.AddParameters(0, "@tran_seq", info.tran_seq);
                dbManager.AddParameters(1, "@account_seq", info.account_seq);
                dbManager.AddParameters(2, "@dr_or_cr", info.dr_or_cr);
                dbManager.AddParameters(3, "@amount", info.amount);
                dbManager.AddParameters(4, "@record_status", info.record_status);
                dbManager.AddParameters(5, "@created_by", info.created_by);
                dbManager.AddParameters(6, "@created_when", info.created_when);

                strSQL = " INSERT INTO acm_transection_dtls "
                        + "(tran_seq,account_seq,dr_or_cr,amount,record_status,created_by,created_when)"
                        + " VALUES (@tran_seq,@account_seq,@dr_or_cr,@amount,@record_status,@created_by,@created_when) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                //dbManager.Dispose();
            }
        }

        public string Insert_Transaction(M_ACM_GENERAL_Ledger info, DBManager dbManager)
        {
            string strSQL;
            string strSeq;
            try
            {
                //dbManager.Open();
                dbManager.CreateParameters(14);
                //dbManager.BeginTransaction();
                dbManager.AddParameters(0, "@fiscal_period", info.fiscal_period);
                dbManager.AddParameters(1, "@tran_grp_seq", info.tran_grp_seq);
                dbManager.AddParameters(2, "@tran_code", info.tran_code);
                dbManager.AddParameters(3, "@tran_date", info.tran_date);
                dbManager.AddParameters(4, "@work_area_seq", info.work_area_seq);
                dbManager.AddParameters(5, "@bgm_source_seq", info.bgm_source_seq);
                dbManager.AddParameters(6, "@tran_name", info.tran_name);
                dbManager.AddParameters(7, "@remarks", info.remark);
                dbManager.AddParameters(8, "@record_status", info.record_status);
                dbManager.AddParameters(9, "@created_by", info.created_by);
                dbManager.AddParameters(10, "@created_when", info.created_when);
                dbManager.AddParameters(11, "@tran_type", info.tran_type);
                dbManager.AddParameters(12, "@ref_no", info.ref_no);
                dbManager.AddParameters(13, "@bgm_year", info.bgm_year);

                strSQL = " INSERT INTO acm_transection "
                        + " (fiscal_period,tran_grp_seq,tran_code,tran_date,work_area_seq,bgm_source_seq "
                        + " ,tran_name,remarks,record_status,created_by,created_when,tran_type,ref_no, bgm_year)"
                        + "  VALUES "
                        + " (@fiscal_period,@tran_grp_seq,@tran_code,@tran_date,@work_area_seq,@bgm_source_seq "
                        + " ,@tran_name,@remarks,@record_status,@created_by,@created_when,@tran_type,@ref_no, @bgm_year)"
                        + " select scope_identity();";
                strSeq = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0][0].ToString();
                //dbManager.ExecuteNonQuery(CommandType.Text, strSQL);

                //strSQL = "select last_insert_id() as last_seq from acm_transection";
                //strSQL = "INSERT INTO icm_bid_offer_collateral"
                //    + " (icm_col_type_seq, icm_bid_off_col_ref_no, icm_bid_off_col_amount"
                //    + ", icm_bid_off_col_descr, icm_bid_off_seq, receipt_no"
                //    + ", created_by, created_when)"
                //    + " VALUES(@icm_col_type_seq"
                //    + ", @icm_bid_off_col_ref_no"
                //    + ", @icm_bid_off_col_amount"
                //    + ", @icm_bid_off_col_descr"
                //    + ", @icm_bid_off_seq"
                //    + ", @receipt_no"

                //    + ", @created_by"
                //    + ", @created_when"
                //    + " );"
                //    + " select scope_identity();";
                //var strSeq = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0][0].ToString();
                //return strSeq;

                //strSeq = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0]["last_seq"].ToString();
                //dbManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                //dbManager.RollBackTransaction();
                throw (ex);
            }
            finally
            {
                //dbManager.Dispose();
            }

            return strSeq;
        }

        public M_ACM_GENERAL_Ledger GetHeaderInfo(int tran_seq, DBManager dbManager)
        {
            DataRowCollection drc = null;
            M_ACM_GENERAL_Ledger info = new M_ACM_GENERAL_Ledger();
            try
            {
                //dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@tran_seq", tran_seq);

                string strSQL = "SELECT bgm_year, tran_code, tran_seq, tran_type, work_area_seq"
                    + ", bgm_source_seq, tran_date, tran_name, remarks, record_status"
                    + ", case record_status"
                        + " when 'A' then 'Posted'"
                        + " else 'รอ Post'"
                        + " end as record_status_name"
                    + " FROM acm_transection"
                    + " WHERE tran_seq = @tran_seq";


                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.tran_seq = Convert.ToInt32(drc[0]["tran_seq"]);
                    info.bgm_year = main.ConvertNullInt(drc[0]["bgm_year"]);
                    info.tran_code = drc[0]["tran_code"].ToString();
                    info.tran_type = drc[0]["tran_type"].ToString();
                    info.work_area_seq = main.ConvertNullInt(drc[0]["work_area_seq"]);
                    info.bgm_source_seq = main.ConvertNullInt(drc[0]["bgm_source_seq"]);
                    info.tran_date = main.ConvertNullDateTime(drc[0]["tran_date"]);
                    info.tran_name = drc[0]["tran_name"].ToString();
                    info.remark = drc[0]["remarks"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                    info.record_status_name = drc[0]["record_status_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                //dbManager.Dispose();
            }
            return info;
        }

        public IList<M_ACM_GENERAL_Ledger> GetDtlsFullData(Nullable<int> tran_seq, DBManager dbManager)
        {
            DataRowCollection drc = null;
            IList<M_ACM_GENERAL_Ledger> infoList = new List<M_ACM_GENERAL_Ledger>();
            try
            {
                //dbManager.Open();
                string strSQL = "";
                dbManager.CreateParameters(2);
                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                //dbManager.AddParameters(1, "@bgm_year", bgm_year);
                //dbManager.AddParameters(2, "@bgm_source_seq", bgm_source_seq);
                //dbManager.AddParameters(3, "@work_area_seq", work_area_seq);
                //dbManager.AddParameters(4, "@tran_type", tran_type);
                //dbManager.AddParameters(5, "@tran_code", tran_code);
                //dbManager.AddParameters(6, "@dtStart", dtStart);
                //dbManager.AddParameters(7, "@dtEnd", dtEnd);
                dbManager.AddParameters(1, "@tran_seq", tran_seq);

                strSQL = "SELECT td.tran_dtls_seq, td.tran_seq" //, t.fiscal_period, t.tran_grp_seq"
                    //+ ", t.tran_code, t.tran_type, t.tran_date, t.tran_name, t.bgm_year, t.work_area_seq, t.bgm_source_seq"
                    + ", td.account_seq, td.dr_or_cr, acc.account_code, acc.account_name, td.amount"
                    + ", case td.dr_or_cr"
                        + " when 'Dr' then td.amount"
                        + " when 'Cr' then null"
                        + " end as debit_amount"
                    + ", case td.dr_or_cr"
                        + " when 'Dr' then null"
                        + " when 'Cr' then td.amount"
                        + " end as credit_amount"
                    + " FROM acm_transection_dtls td"
                    //+ " Left Join acm_transection_dtls td ON t.tran_seq = td.tran_seq and td.record_status not like @delete_status"
                    + " Left Join acm_mas_account acc ON td.account_seq = acc.account_seq"
                    + " WHERE td.record_status not like @delete_status"
                    //+ (bgm_year != null ? " and t.bgm_year = @bgm_year" : "")
                    //+ (bgm_source_seq != null ? " and t.bgm_source_seq = @bgm_source_seq" : "")
                    //+ (work_area_seq != null ? " and t.work_area_seq = @work_area_seq" : "")
                    //+ (!string.IsNullOrEmpty(tran_type) ? " and t.tran_type = @tran_type" : "")
                    //+ (!string.IsNullOrEmpty(tran_code) ? " and t.tran_code like '%" + tran_code + "%'" : "")
                    //+ (dtStart != null ? " and t.tran_date >= @dtStart" : "")
                    //+ (dtEnd != null ? " and t.tran_date <= @dtEnd" : "")
                    + (tran_seq != null ? " and td.tran_seq = @tran_seq" : "")

                    + " ORDER BY td.tran_dtls_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_GENERAL_Ledger info = new M_ACM_GENERAL_Ledger();
                        info.tran_dtls_seq = main.ConvertNullInt(drc[i]["tran_dtls_seq"]);
                        info.tran_seq = Convert.ToInt32(drc[i]["tran_seq"]);
                        //info.tran_type = drc[i]["tran_type"].ToString();
                        info.account_seq = main.ConvertNullInt(drc[i]["account_seq"]);
                        info.account_code = drc[i]["account_code"].ToString();
                        info.account_name = drc[i]["account_name"].ToString();
                        //info.tran_code = drc[i]["tran_code"].ToString();
                        //info.tran_date = cn.ConvertNullDateTime(drc[i]["tran_date"]);
                        //info.tran_name = drc[i]["tran_name"].ToString();
                        info.dr_or_cr = drc[i]["dr_or_cr"].ToString();
                        info.amount = main.ConvertNullDouble(drc[i]["amount"]);
                        info.debit_amount = main.ConvertNullDouble(drc[i]["debit_amount"]);
                        info.credit_amount = main.ConvertNullDouble(drc[i]["credit_amount"]);
                        //info.bgm_source_seq = cn.ConvertNullInt(drc[i]["account_seq"]);
                        //info.bgm_year = cn.ConvertNullInt(drc[i]["bgm_year"]);
                        //info.work_area_seq = cn.ConvertNullInt(drc[i]["work_area_seq"]);
                        //info.fiscal_period = drc[i]["fiscal_period"].ToString();
                        //info.tran_grp_seq = cn.ConvertNullInt(drc[i]["tran_grp_seq"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                //dbManager.Dispose();
            }
            return infoList;
        }

        public string GetNextTranCode(string bgm_source_seq, string bgm_year, string tran_type, DBManager dbManager)
        {
            DataTable dtb = null;
            string strNextTranCode = tran_type + "-" + (!string.IsNullOrEmpty(bgm_source_seq) ? (bgm_source_seq == "1" ? "1" : "2") : "3") + "-" + bgm_year + "-00001";
            try
            {
                //dbManager.Open();

                string strSQL = "SELECT max(CONVERT(VARCHAR(50),tran_code)) as tran_code"
                    + " from acm_transection"
                    + " where record_status not like 'D'"
                    + " and bgm_source_seq = " + (!string.IsNullOrEmpty(bgm_source_seq) ? bgm_source_seq : "1")
                    + " and bgm_year = " + bgm_year
                    + " and tran_type = '" + tran_type + "'"

                    + " group by bgm_source_seq, bgm_year, tran_type";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    if (dtb.Rows[0]["tran_code"] != DBNull.Value)
                    {
                        strNextTranCode = tran_type + "-" + (!string.IsNullOrEmpty(bgm_source_seq) ? (bgm_source_seq == "1" ? "1" : "2") : "3") + "-" + bgm_year + "-" + (Convert.ToInt32(dtb.Rows[0]["tran_code"].ToString().Split('-')[2]) + 1).ToString("00000");
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                //dbManager.Dispose();
            }
            return strNextTranCode;
        }
    }

    public class M_ACM_GENERATE_ACCOUNT_Auto
    {
        private string REF_TYPE;
        private string TRAN_TYPE;
        private string REF_NO;
        private Nullable<DateTime> TRAN_DATE;
        private string TRAN_NAME;
        private Nullable<Int32> WORK_AREA_SEQ;
        private Nullable<Int32> BGM_SOURCE_SEQ;
        private Nullable<Int32> REF_SEQ;
        private string SOURCE_NAME;
        private string AREA_NAME;
        private Nullable<int> BGM_YEAR;
        //Detail
        private Nullable<Int32> ACCOUNT_SEQ;
        private string DR_OR_CR;
        private Nullable<double> AMOUNT;
        private Nullable<double> AMOUNT_DR;
        private Nullable<double> AMOUNT_CR;
        //Add Detail
        private Nullable<Int32> TRAN_GRP_SEQ;
        private string TRAN_GRP_CODE;
        private string RECORD_STATUS;
        private Int32 CREATED_BY;
        private DateTime CREATED_WHEN;
        private Nullable<Int32> UPDATED_BY;
        private Nullable<DateTime> UPDATED_WHEN;
        private string ACCOUNT_NAME;
        private string ACCOUNT_CODE;


        public M_ACM_GENERATE_ACCOUNT_Auto()
        {
        }

        public string ref_type
        {
            get { return REF_TYPE; }
            set { REF_TYPE = value; }
        }
        public string tran_type
        {
            get { return TRAN_TYPE; }
            set { TRAN_TYPE = value; }
        }
        public string ref_no
        {
            get { return REF_NO; }
            set { REF_NO = value; }
        }
        public Nullable<DateTime> tran_date
        {
            get { return TRAN_DATE; }
            set { TRAN_DATE = value; }
        }
        public string tran_name
        {
            get { return TRAN_NAME; }
            set { TRAN_NAME = value; }
        }
        public Nullable<Int32> work_area_seq
        {
            get { return WORK_AREA_SEQ; }
            set { WORK_AREA_SEQ = value; }
        }
        public Nullable<Int32> bgm_source_seq
        {
            get { return BGM_SOURCE_SEQ; }
            set { BGM_SOURCE_SEQ = value; }
        }
        public Nullable<Int32> ref_seq
        {
            get { return REF_SEQ; }
            set { REF_SEQ = value; }
        }
        public string source_name
        {
            get { return SOURCE_NAME; }
            set { SOURCE_NAME = value; }
        }
        public string area_name
        {
            get { return AREA_NAME; }
            set { AREA_NAME = value; }
        }
        //Detail
        public Nullable<Int32> account_seq
        {
            get { return ACCOUNT_SEQ; }
            set { ACCOUNT_SEQ = value; }
        }
        public string dr_or_cr
        {
            get { return DR_OR_CR; }
            set { DR_OR_CR = value; }
        }
        public Nullable<double> amount
        {
            get { return AMOUNT; }
            set { AMOUNT = value; }
        }
        public Nullable<double> amount_dr
        {
            get { return AMOUNT_DR; }
            set { AMOUNT_DR = value; }
        }
        public Nullable<double> amount_cr
        {
            get { return AMOUNT_CR; }
            set { AMOUNT_CR = value; }
        }
        public Nullable<Int32> tran_grp_seq
        {
            get { return TRAN_GRP_SEQ; }
            set { TRAN_GRP_SEQ = value; }
        }
        public string tran_grp_code
        {
            get { return TRAN_GRP_CODE; }
            set { TRAN_GRP_CODE = value; }
        }
        public string record_status
        {
            get { return RECORD_STATUS; }
            set { RECORD_STATUS = value; }
        }
        public Int32 created_by
        {
            get { return CREATED_BY; }
            set { CREATED_BY = value; }
        }
        public DateTime created_when
        {
            get { return CREATED_WHEN; }
            set { CREATED_WHEN = value; }
        }
        public Nullable<Int32> updated_by
        {
            get { return UPDATED_BY; }
            set { UPDATED_BY = value; }
        }
        public Nullable<DateTime> updated_when
        {
            get { return UPDATED_WHEN; }
            set { UPDATED_WHEN = value; }
        }
        public string account_name
        {
            get { return ACCOUNT_NAME; }
            set { ACCOUNT_NAME = value; }
        }

        public string account_code
        {
            get { return ACCOUNT_CODE; }
            set { ACCOUNT_CODE = value; }
        }

        public Nullable<int> bgm_year
        {
            get { return BGM_YEAR; }
            set { BGM_YEAR = value; }
        }
    }

    public class M_ACM_GENERAL_Ledger
    {
        //acm_transection
        private Int32 TRAN_SEQ;
        private string FISCAL_PERIOD;//fiscal_period;
        private Nullable<Int32> TRAN_GRP_SEQ;
        private string TRAN_CODE;
        private Nullable<DateTime> TRAN_DATE;
        private Nullable<Int32> WORK_AREA_SEQ;
        private Nullable<Int32> BGM_SOURCE_SEQ;
        private string TRAN_NAME;
        private string REMARK;
        private Nullable<Int32> APPROVED_BY;
        private DateTime APPROVED_WHEN;
        private string TRAN_TYPE;
        private string REF_NO;
        private Nullable<int> BGM_YEAR;

        //acm_transection_dtls
        private Nullable<Int32> TRAN_DTLS_SEQ;
        private Nullable<Int32> ACCOUNT_SEQ;
        private string DR_OR_CR;
        private Nullable<double> AMOUNT;
        private string RECORD_STATUS;
        private Int32 CREATED_BY;
        private DateTime CREATED_WHEN;
        private Nullable<Int32> UPDATED_BY;
        private DateTime UPDATED_WHEN;

        //acm_transection_group
        private string TRAN_GRP_CODE;
        private string REF_TYPE;

        //fim_mas_record_statuses
        private string RECORD_STATUS_CODE;//record_status_code;
        private string RECORD_STATUS_NAME;//record_status_name;

        //for cmbSourceMoney
        private string BGM_SOURCE_NAME;

        //Add
        private string ACCOUNT_CODE;
        private string ACCOUNT_NAME;
        private Nullable<double> DEBIT_AMOUNT;
        private Nullable<double> CREDIT_AMOUNT;
        private Nullable<Boolean> APPROVE_STATUS;

        public M_ACM_GENERAL_Ledger(Int32 TRAN_SEQ, string FISCAL_PERIOD, Nullable<Int32> TRAN_GRP_SEQ, string TRAN_CODE
               , Nullable<DateTime> TRAN_DATE, Nullable<Int32> WORK_AREA_SEQ, Nullable<Int32> BGM_SOURCE_SEQ, string BGM_SOURCE_NAME, string TRAN_NAME
               , string REMARK, Nullable<Int32> APPROVED_BY, DateTime APPROVED_WHEN, string TRAN_TYPE, string REF_NO, Nullable<int> BGM_YEAR, Nullable<Int32> TRAN_DTLS_SEQ
               , Nullable<Int32> ACCOUNT_SEQ, string DR_OR_CR, Nullable<double> AMOUNT, string RECORD_STATUS, Int32 CREATED_BY
               , DateTime CREATED_WHEN, Nullable<Int32> UPDATED_BY, DateTime UPDATED_WHEN, string TRAN_GRP_CODE, string REF_TYPE
               , string ACCOUNT_NAME, Nullable<double> DEBIT_AMOUNT, Nullable<double> CREDIT_AMOUNT, Nullable<Boolean> APPROVE_STATUS
               , string RECORD_STATUS_CODE, string RECORD_STATUS_NAME, string ACCOUNT_CODE)
        {
            this.TRAN_SEQ = TRAN_SEQ;
            this.FISCAL_PERIOD = FISCAL_PERIOD;
            this.TRAN_GRP_SEQ = TRAN_GRP_SEQ;
            this.TRAN_CODE = TRAN_CODE;
            this.TRAN_DATE = TRAN_DATE;
            this.WORK_AREA_SEQ = WORK_AREA_SEQ;
            this.BGM_SOURCE_SEQ = BGM_SOURCE_SEQ;
            this.BGM_SOURCE_NAME = BGM_SOURCE_NAME;
            this.TRAN_NAME = TRAN_NAME;
            this.REMARK = REMARK;
            this.APPROVED_BY = APPROVED_BY;
            this.APPROVED_WHEN = APPROVED_WHEN;
            this.TRAN_TYPE = TRAN_TYPE;
            this.REF_NO = REF_NO;
            this.TRAN_DTLS_SEQ = TRAN_DTLS_SEQ;
            this.ACCOUNT_SEQ = ACCOUNT_SEQ;
            this.DR_OR_CR = DR_OR_CR;
            this.AMOUNT = AMOUNT;
            this.RECORD_STATUS = RECORD_STATUS;
            this.CREATED_BY = CREATED_BY;
            this.CREATED_WHEN = CREATED_WHEN;
            this.UPDATED_BY = UPDATED_BY;
            this.UPDATED_WHEN = UPDATED_WHEN;
            this.TRAN_GRP_CODE = TRAN_GRP_CODE;
            this.REF_TYPE = REF_TYPE;
            this.ACCOUNT_NAME = ACCOUNT_NAME;
            this.DEBIT_AMOUNT = DEBIT_AMOUNT;
            this.CREDIT_AMOUNT = CREDIT_AMOUNT;
            this.APPROVE_STATUS = APPROVE_STATUS;
            this.RECORD_STATUS_CODE = RECORD_STATUS_CODE;
            this.RECORD_STATUS_NAME = RECORD_STATUS_NAME;
            this.BGM_YEAR = BGM_YEAR;
            this.ACCOUNT_CODE = ACCOUNT_CODE;
        }
        public M_ACM_GENERAL_Ledger()
        {
        }


        public Int32 tran_seq
        {
            get { return TRAN_SEQ; }
            set { TRAN_SEQ = value; }
        }
        public string fiscal_period
        {
            get { return FISCAL_PERIOD; }
            set { FISCAL_PERIOD = value; }
        }
        public Nullable<Int32> tran_grp_seq
        {
            get { return TRAN_GRP_SEQ; }
            set { TRAN_GRP_SEQ = value; }
        }
        public string tran_code
        {
            get { return TRAN_CODE; }
            set { TRAN_CODE = value; }
        }
        public Nullable<DateTime> tran_date
        {
            get { return TRAN_DATE; }
            set { TRAN_DATE = value; }
        }
        public Nullable<Int32> work_area_seq
        {
            get { return WORK_AREA_SEQ; }
            set { WORK_AREA_SEQ = value; }
        }
        public Nullable<Int32> bgm_source_seq
        {
            get { return BGM_SOURCE_SEQ; }
            set { BGM_SOURCE_SEQ = value; }
        }
        public string tran_name
        {
            get { return TRAN_NAME; }
            set { TRAN_NAME = value; }
        }
        public string remark
        {
            get { return REMARK; }
            set { REMARK = value; }
        }
        public Nullable<Int32> approved_by
        {
            get { return APPROVED_BY; }
            set { APPROVED_BY = value; }
        }
        public DateTime approved_when
        {
            get { return APPROVED_WHEN; }
            set { APPROVED_WHEN = value; }
        }
        public string tran_type
        {
            get { return TRAN_TYPE; }
            set { TRAN_TYPE = value; }
        }
        public Nullable<Int32> tran_dtls_seq
        {
            get { return TRAN_DTLS_SEQ; }
            set { TRAN_DTLS_SEQ = value; }
        }
        public string ref_no
        {
            get { return REF_NO; }
            set { REF_NO = value; }
        }
        public Nullable<Int32> account_seq
        {
            get { return ACCOUNT_SEQ; }
            set { ACCOUNT_SEQ = value; }
        }
        public string dr_or_cr
        {
            get { return DR_OR_CR; }
            set { DR_OR_CR = value; }
        }
        public Nullable<double> amount
        {
            get { return AMOUNT; }
            set { AMOUNT = value; }
        }
        public string record_status
        {
            get { return RECORD_STATUS; }
            set { RECORD_STATUS = value; }
        }
        public Int32 created_by
        {
            get { return CREATED_BY; }
            set { CREATED_BY = value; }
        }
        public DateTime created_when
        {
            get { return CREATED_WHEN; }
            set { CREATED_WHEN = value; }
        }
        public Nullable<Int32> updated_by
        {
            get { return UPDATED_BY; }
            set { UPDATED_BY = value; }
        }
        public string tran_grp_code
        {
            get { return TRAN_GRP_CODE; }
            set { TRAN_GRP_CODE = value; }
        }
        public DateTime updated_when
        {
            get { return UPDATED_WHEN; }
            set { UPDATED_WHEN = value; }
        }
        public string ref_type
        {
            get { return REF_TYPE; }
            set { REF_TYPE = value; }
        }
        public string bgm_source_name
        {
            get { return BGM_SOURCE_NAME; }
            set { BGM_SOURCE_NAME = value; }
        }
        public string account_name
        {
            get { return ACCOUNT_NAME; }
            set { ACCOUNT_NAME = value; }
        }
        public Nullable<double> debit_amount
        {
            get { return DEBIT_AMOUNT; }
            set { DEBIT_AMOUNT = value; }
        }
        public Nullable<double> credit_amount
        {
            get { return CREDIT_AMOUNT; }
            set { CREDIT_AMOUNT = value; }
        }
        public Nullable<Boolean> approve_status
        {
            get { return APPROVE_STATUS; }
            set { APPROVE_STATUS = value; }
        }
        //fim_mas_record_statuses Show status of account transfer
        public string record_status_code
        {
            get { return RECORD_STATUS_CODE; }
            set { RECORD_STATUS_CODE = value; }
        }
        public string record_status_name
        {
            get { return RECORD_STATUS_NAME; }
            set { RECORD_STATUS_NAME = value; }
        }
        public string account_code
        {
            get { return ACCOUNT_CODE; }
            set { ACCOUNT_CODE = value; }
        }

        public Nullable<int> bgm_year
        {
            get { return BGM_YEAR; }
            set { BGM_YEAR = value; }
        }
    }
}